package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Ygzc;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class YgzcForm extends BasePage implements Serializable {
    private GenericManager<Ygzc, Long> ygzcManager;
    private Ygzc ygzc = new Ygzc();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}
    public void setYgzcManager(GenericManager<Ygzc, Long> ygzcManager) {
        this.ygzcManager = ygzcManager;
    }

    public Ygzc getYgzc() {
        return ygzc;
    }

    public void setYgzc(Ygzc ygzc) {
        this.ygzc = ygzc;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        ygzcManager.remove(ygzc.getId());
        addMessage("ygzc.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            ygzc = ygzcManager.get(id);
        } else {
            ygzc = new Ygzc();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (ygzc.getId() == null || ygzc.getId() == 0);
        
        ygzc.setJbqk(jbqk);
        ygzc.setUsername(getRequest().getRemoteUser());
        ygzc.setOptdate(new Date());
        ygzc.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        
        ygzcManager.save(ygzc);

        String key = (isNew) ? "ygzc.added" : "ygzc.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
           	
     	   this.id=this.ygzc.getId();
              return edit();
        }
    }
} 